﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 选项框单元格的内容
/// </summary>
public class OptionCellContent : CellContent {
    public Dropdown dropdown;
    //public OptionContent_String Value;
    public List<OptionContent_String> Values = new List<OptionContent_String>();
    public OptionCellContent()
    {
        description = "选项框";
        contentType = CellContentType.DropDown;
    }

    public override void OnSelected(bool b)
    {
        SheetSystemManager.sheetSystemManager.optionsManager.CheckOptionCellContent();
    }
    public void SetOption(OptionContent_String option,bool add)
    {
        #region//单选
        //if (Value != option)
        //{
        //    if (Value != null)
        //    {
        //        Value.onDescriptionChanged -= SetDescription;
        //    }
        //    if (option != null)
        //    {
        //        Value = option;
        //        description = Value.description;
        //        Value.onDescriptionChanged += SetDescription;
        //    }
        //    else
        //    {
        //        description = "选项框";
        //    }
        //}
        #endregion

        #region//多选
        if (add)
        {
            if ( !(Values.Count > 0 && Values[0].myOption == option.myOption))
            {
                foreach (var item in Values)
                {
                    item.onDescriptionChanged -= SetDescription;
                }
                Values.Clear();
            }
            if (!Values.Contains(option))
            {
                Values.Add(option);
                option.onDescriptionChanged += SetDescription;
                //Values.Sort(delegate (OptionContent_String a, OptionContent_String b) { return a.index.CompareTo(b.index); });
                SetDescription("");
            }
        }
        else
        {
            option.onDescriptionChanged -= SetDescription;
            Values.Remove(option);
            //Values.Sort(delegate (OptionContent_String a, OptionContent_String b) { return a.index.CompareTo(b.index); });
            SetDescription("");
        }
        #endregion
    }
    
    void SetDescription(string s)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < Values.Count; i++)
        {
            sb.AppendLine(i + ":" + Values[i].description);
        }
        description = sb.ToString();
    }
}
